package us.mooresr.webtroopmanager.client.activity;

import java.util.List;

import us.mooresr.webtroopmanager.client.ClientFactory;
import us.mooresr.webtroopmanager.client.TroopManagerService;
import us.mooresr.webtroopmanager.client.TroopManagerServiceAsync;
import us.mooresr.webtroopmanager.client.events.VehicleAddedEvent;
import us.mooresr.webtroopmanager.client.place.EditVehiclePlace;
import us.mooresr.webtroopmanager.client.ui.EditVehicleView;
import us.mooresr.webtroopmanager.shared.PersonSummary;
import us.mooresr.webtroopmanager.shared.Vehicle;
import us.mooresr.webtroopmanager.shared.VehicleSummary;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.AcceptsOneWidget;

public class EditVehicleActivity extends MyActivity implements EditVehicleView.Presenter {
	
	public EditVehicleActivity(EditVehiclePlace place,
			ClientFactory clientFactory) {
		super(place, clientFactory);
	}


	@Override
	public void start(AcceptsOneWidget panel, EventBus eventBus) {
		super.start(panel, eventBus);
		EditVehicleView editVehicleView = clientFactory.getEditVehicleView();
		editVehicleView.setPresenter(this);
		getVehicle(((EditVehiclePlace)place).getVehicleId());
		populateComboBoxes();
		panel.setWidget(editVehicleView.asWidget());
		
	}


	@Override
	public void addVehicle(Vehicle v) {
		TroopManagerServiceAsync troopManagerService = (TroopManagerServiceAsync) GWT.create(TroopManagerService.class);
		
		
		AsyncCallback<VehicleSummary> callback = new AsyncCallback<VehicleSummary>() {

			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onSuccess(VehicleSummary result) {
				
				clientFactory.getEventBus().fireEvent(new VehicleAddedEvent(result));

			}
		};
		
		troopManagerService.addVehicle(v, callback);
		
	}
	
	private void getVehicle(Long vehicleId) {
		TroopManagerServiceAsync troopManagerService = (TroopManagerServiceAsync) GWT.create(TroopManagerService.class);
		
		if (vehicleId == null) {
			clientFactory.getEditVehicleView().setData(null);
			return;
		}
		
		AsyncCallback<Vehicle> callback = new AsyncCallback<Vehicle>() {
			@Override
			public void onFailure(Throwable caught) {
				clientFactory.getEditVehicleView().setData(null);
				
			}
			
			@Override
			public void onSuccess(Vehicle result) {
				clientFactory.getEditVehicleView().setData(result);
				
			}
		};
		
		troopManagerService.getVehicle(vehicleId, callback);

	}
	
	private void populateComboBoxes() {
		TroopManagerServiceAsync troopManagerService = (TroopManagerServiceAsync) GWT.create(TroopManagerService.class);

		AsyncCallback<List<PersonSummary>> callback = new AsyncCallback<List<PersonSummary>>() {
			@Override
			public void onFailure(Throwable caught) {
			}

			@Override
			public void onSuccess(List<PersonSummary> result) {
				clientFactory.getEditVehicleView().setComboBoxes(result);
			}
		};

		troopManagerService.getAllAdults(callback);
	}

}
